package com.niit.controller;

import com.niit.pojo.Dish;
import com.niit.pojo.Dish;
import com.niit.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * DishController 控制器类，用于处理与消费者相关的操作
 */
@Controller
public class DishController {

    @Autowired
    private DishService dishService; // 自动注入 DishService 依赖


    @GetMapping("/backend-api-endpoint/getAllDishs")
    @ResponseBody
    public List<Dish> getAllDishs() {
        // 调用服务从数据库获取数据
        List<Dish> dishs = dishService.getAllDishs();
        return dishs;
    }


    @RequestMapping("/dishes_table")
    public String dishes_table(){
        return "dishes_table";
    }


    @PostMapping("/backend-api-endpoint/addDish")
    @ResponseBody
    public ResponseEntity<String> addDish(@RequestBody Dish dish) {
        try {
            // 在这里处理新增菜品的逻辑，将 dish 对象保存到数据库
            dishService.addDish(dish);
            return ResponseEntity.ok("菜品添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("添加菜品时出错");
        }
    }



    @PostMapping("/backend-api-endpoint/editDish")
    @ResponseBody
    public ResponseEntity<String> editDish(@RequestBody Dish dish) {
        try {
            dishService.editDish(dish);
            return ResponseEntity.ok("菜品编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("编辑菜品时出错");
        }
    }



    @PostMapping("/backend-api-endpoint/deleteDish")
    @ResponseBody
    public ResponseEntity<String> deleteDish(@RequestBody Map<String, String> requestData) {
        String dishId = requestData.get("dish_id");

        try {
            dishService.deleteDish(dishId);
            return ResponseEntity.ok("菜品删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除菜品时出错");
        }
    }


    @GetMapping("/backend-api-endpoint/getDishById")
    @ResponseBody
    public ResponseEntity<Dish> getDishById(@RequestParam String dishId) {
        try {
            Dish dish = dishService.getDishById(dishId);
            System.out.println("Retrieved dish: " + dish);
            return ResponseEntity.ok(dish);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @GetMapping("/backend-api-endpoint/getDishByWindowId")
    @ResponseBody
    public ResponseEntity<List<Dish>>getDishById(HttpSession session) {
        String window_id = (String) session.getAttribute("window");
        try {
         List<Dish> dish = dishService.getDishByWindowId(window_id);
            return ResponseEntity.ok(dish);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }




    @GetMapping("/edit_dish_with_params")
    public String editDishPage(Model model, @RequestParam String dishId) {
        // 假设你在 DishService 中有一个通过 ID 获取消费者的方法
        Dish dish = dishService.getDishById(dishId);
        // 将 dish 对象添加到模型中
        model.addAttribute("dish", dish);
        model.addAttribute("dishId", dishId);
        // 返回 Thymeleaf 模板名称
        return "edit_dish";
    }





    }







